<?php
namespace Api\Controller;

use Api\Controller\V2\V2BaseController;
use Common\Util\RSA;

// +----------------------------------------------------------------------
// | 文件 V2Controller.class.php
// +----------------------------------------------------------------------
// | 说明 v2.0版本的接口
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

class V2Controller
{

    const MOD_EVENT = 'V2';
    const TIMESTAMP_TIMEOUT = true;

    public function __construct()
    {
        $timeout = 5;
        $sign_type = I('_sign_type', '', 'strtoupper');
        if (IS_POST){

            // RSA 解密
            if ($sign_type == 'RSA'){
                $RSA = new RSA(C('RSA_PUBLIC_KEY'), C('RSA_PRIVATE_KEY'));
                $content = I('sign');
                $request = $RSA->privateDecrypt($content)->toArray();
                if (!$request) {
                    error(400, '请求失败');
                }
                request($request);
            }

            // 时间戳验证
            if (static::TIMESTAMP_TIMEOUT && (!request('timestamp') || abs(request('timestamp') - time()) > $timeout ) ){
                error(400, '本机时间异常');
            }


        }
        
        
    }

    // 所有的接口按照action参数实现 action=video/index 转发到v2组去
    public function index()
    {
        $r = request('r');
        if (!$r) {
            error(404);
        }
        $mod = explode('/', $r);
        if (!$mod || count($mod) < 1){
            error(404);
        }
        $c      = ucfirst($mod[0]);
        $action = isset($mod[1]) ? $mod[1] : 'index';

        $class = "Api\\Controller\\V2\\{$c}Controller";
        if (class_exists($class)){
            $controller = new $class;
            if ($controller instanceof V2BaseController){
                $controller->{$action}();
                // exit;
            }
        }
        // error(404);
    }

    public function __call($methid, $args)
    {
        error(404);
    }

}